<!--  -->
<template>
  <div class="header">
    <div class="title">
      <img src="../../assets/温州银行.png" alt width="100px" />
      <p :style="`font-weight: 600;color:${this.$global.headTitleColor}`">{{title}}</p>
    </div>
    <el-menu
      :default-active="activeIndex"
      class="el-menu-demo"
      mode="horizontal"
      @select="handleSelect"
      :background-color="this.$global.bgColor"
      :text-color="this.$global.headNavColor"
      :active-text-color="this.$global.headTitleColor"
      router
    >
      <!-- 导航栏菜单 -->
      <el-menu-item
        v-for="(item,index) in routes"
        :key="index"
        :index="item.path"
      >{{item.meta.title}}</el-menu-item>
    </el-menu>
    <div class="user">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          欢迎您，{{username}}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="1">修改密码</el-dropdown-item>
          <el-dropdown-item command="2">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
export default {
  name: "Header",
  data() {
    return {
      routes: [],
      activeIndex: "/Workdesktop", //当前路由菜单名称 默认首页
      title: process.env.VUE_APP_NAME
    };
  },
  computed: {
    username() {
      if (this.$store.state.login.username) {
        return this.$store.state.login.username;
      } else {
        return localStorage.getItem("userInfo")
          ? JSON.parse(localStorage.getItem("userInfo")).username
          : "";
      }
    }
  },
  mounted() {
    // routerName 获取当前菜单 防止页面刷新重置
    this.activeIndex = localStorage.getItem("routerName") || "/Workdesktop";
    // 获取路由
    this.routes = this.$router.options.routes[2].children;
  },
  //事件方法
  methods: {
    // 选择菜单
    handleSelect(key, keyPath) {
      //存入本地 防止页面刷新菜单重置 退出登录删除
      localStorage.setItem("routerName", key);
    },
    // 下拉菜单事件
    handleCommand(command) {
      switch (command) {
        case "1":
          break;
        case "2":
          this.quitLogin();
          break;
        default:
          break;
      }
    },
    // 退出登录
    quitLogin() {
      window.location.reload();
      localStorage.clear();
    }
  }
};
</script>
<style scoped lang='scss'>
/* @import url(); 引入css类 */
.title {
  display: flex;
  height: 60px;
  align-items: center;
  img {
    margin-right: 10px;
  }
}
.header {
  display: flex;
  justify-content: space-between;
}

.el-dropdown-link {
  cursor: pointer;
  color: #faa61a;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>